package club.banyuan.hrms.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ServletTest2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置content-type
        response.setHeader("Content-Type", "text/html;charset=utf-8");
//        response.setContentType("text/html;charset=utf-8");

        // 取头部信息
        String userAgent = request.getHeader("User-Agent");
        String host = request.getHeader("Host");

        String cookie = request.getHeader("Cookie");
        String accept = request.getHeader("Accept");
        String connection = request.getHeader("Connection");

        String cacheControl = request.getHeader("Cache-Control");

        StringBuffer sb = new StringBuffer();
        sb.append("<html><body>").append("<div>UserAgent: ").append(userAgent).append("</div>");
        sb.append("<div>").append("Host: ").append(host).append("</div>");
        sb.append("<div>").append("Cookie: ").append(cookie).append("</div>");
        sb.append("<div>").append("accept: ").append(accept).append("</div>");
        sb.append("<div>").append("connection: ").append(connection).append("</div>");
        sb.append("<div>").append("cacheControl: ").append(cacheControl).append("</div>");

        // 枚举获得头部信息
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = request.getHeader(name);
            sb.append("<div>").append("name: ").append(value).append("</div>");
        }
        sb.append("</body></html>");


        response.getWriter().println(sb.toString());
//        response.getOutputStream().write(sb.toString().getBytes());


//        Cookie cookie1 = new Cookie("cookie","c1");
//        cookie1.setMaxAge(0);
//        response.addCookie(cookie1);
    }
}
